<?php
class ez_Database_Select extends ez_Database_Resource
{
	private $_columns;
	private $_where;
	private $_orderby;
	private $_orderbyType;
	private $_limit;
	private $_innerJoin;
	private $_on;
	private $_groupby;
	
	function ez_Resource($table)
	{
		$this->table($table);
	}
	
	public function table($table)
	{
		$this->_table = $table;
		return $this;
	}
	
	public function columns($columns = array())
	{
		$this->_columns=$columns;
		return $this;
	}
	
	public function where($conditions)
	{
		$this->_conditions = $conditions;
		return $this;
	}
	
	public function orderBy($orderby,$type='')
	{
		$this->_orderby = $orderby;
		$this->_orderbyType = $type;
		return $this;
	}
	
	public function limit($start,$count)
	{
		$this->_limitStart = $start;
		$this->_limitCount = $count;
	}
	
	public function innerJoin($tables,$on)
	{
		$this->_innerJoin = $tables;
		$this->_on = $on;
		return $this;
	}
	
	public function groupBy($groupby) {
		$this->_groupby = $groupby;
		return $this;
	}
	
	
	public function select()
	{
		$columns = '';
		$where = '';
		$orderby = '';
		$groupby = '';
		$limit = '';
		$innerJoin = '';
		
		if (empty($this->_table)) {
			return;
		}
		
		if (empty($this->_columns)) {
			$columnds = '*';
		} else {
			$columnds = ' '.$this->_columns;
		}
		
		if (!empty($this->_where)) {
			$where = ' WHERE '.$this->_where;
		}
		
		if (!empty($this->orderby)) {
			$orderby = ' ORDER BY '.$this->_orderby;
		}
		
		if (!empty($this->_orderbyType)) {
			$orderby .= ' '.$this->orderbyType;
		}
		
		if (!empty($this->_limitStart) && !empty($this->_limitCount)) {
			$limit = " LIMIT($this->_limitStart,$this->_limitCount)";
		}
		
		if (!empty($this->_innerJoin) && !empty($this->_on)) {
			$innerJoin = " INNER JOIN $this->_innerJoin ON $this->_on";
		}
		
		if (!empty($this->_groupby)) {
			$groupby = ' GROUP BY' . $this->_groupby;
		}
		
		$sql = 
			'SELECT'. 
			$columns. 
			' FROM'.
			$this->_table.
			$innerJoin.
			$where.
			$orderby.
			$groupby.
			$limit;
	}
}